package com.lsb.fitnessnutrition

import android.content.Context
import android.database.sqlite.SQLiteDatabase
import android.database.sqlite.SQLiteOpenHelper
import android.os.Build
import android.widget.Toast
import androidx.annotation.RequiresApi

@RequiresApi(Build.VERSION_CODES.P)
class MyDatabaseHelper(
    context: Context?, name: String?, factory: SQLiteDatabase.CursorFactory?, version: Int
) : SQLiteOpenHelper(context, name, factory, version) {

    val CREATE_TABLE:String =
        "create table power(" +
                "id integer primary key autoincrement," +
                "type text," +
                "rm integer);"

    val CREATE_TABLE_PLAN_FIVE:String =
        "create table plan_five(" +
                "id integer primary key autoincrement," +
                "type text," +
                "weight double," +
                "groups text," +
                "status text);"

    val DROP_TABLE_PLAN_FIVE:String =
        "DROP TABLE IF EXISTS plan_five;"

    val CREATE_TABLE_PLAN_TWO:String =
        "create table plan_two(" +
                "id integer primary key autoincrement," +
                "type text," +
                "weight double," +
                "groups text," +
                "status text);"

    val DROP_TABLE_PLAN_TWO:String =
        "DROP TABLE IF EXISTS plan_two;"

    private val myContext: Context

    init{
        this.myContext = context!!
    }

    override fun onCreate(db: SQLiteDatabase?) {
        db!!.execSQL(CREATE_TABLE);
        Toast.makeText(myContext, "create table succeed",
            Toast.LENGTH_SHORT).show()
    }

    override fun onUpgrade(p0: SQLiteDatabase?, p1: Int, p2: Int) {
        TODO("Not yet implemented")
    }

    //  删除数据库
    fun deleteDatabase(context: Context): Boolean {
        return context.deleteDatabase("PowerStore.db")
    }

    fun createTable_plan_five(db: SQLiteDatabase?) {
        db!!.execSQL(DROP_TABLE_PLAN_FIVE)
        db!!.execSQL(CREATE_TABLE_PLAN_FIVE)
    }

    fun createTable_plan_two(db:SQLiteDatabase?){
        db!!.execSQL(DROP_TABLE_PLAN_TWO)
        db!!.execSQL(CREATE_TABLE_PLAN_TWO)
    }
}